package com.jack.thinking.thread.core._2;

import java.util.concurrent.atomic.AtomicLong;

public class AotomictTrap {
	public static void main(String[] args) throws InterruptedException {
		MyService service = new  MyService();
		MyThread [] t = new MyThread[5];
		for (int i = 0; i < t.length; i++) {
			t[i] = new MyThread(service);
		}
		for (int i = 0; i < t.length; i++) {
			t[i].start();
		}
		Thread.sleep(1000);
		System.out.println(MyService.aiRef.get());
	}
}

class MyService{
	public static AtomicLong aiRef = new AtomicLong();
	
	synchronized public void addNum() {
		System.out.println(Thread.currentThread().getName() + "after adding 100：" + aiRef.addAndGet(100));
		aiRef.addAndGet(1);
	}
}

class MyThread extends Thread{
	private MyService myService;

	public MyThread(MyService myService) {
		super();
		this.myService = myService;
	}
	
	@Override
	public void run() {
		myService.addNum();
	}
}